R Palooza with Kelly

gt

gt_parts_of_a_table

Creating a simple table

penguins_summary %>%
  gt() %>%
  tab_header(title = "Palmer Penguin Measurements")
Palmer Penguin Measurements
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Renaming Columns

penguins_summary %>%
  gt() %>%
  tab_header(title = "Palmer Penguin Measurements") %>%
  cols_label(
    species = "Species",
    mean_bill_length = "Bill Length (mean)",
    mean_bill_depth = "Bill Depth (mean)",
    mean_flipper_length = "Flipper Length (mean)"
  )
Palmer Penguin Measurements
Species Bill Length (mean) Bill Depth (mean) Flipper Length (mean)
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Formatting Numbers

penguins_summary %>%
  gt() %>%
  tab_header(title = "Palmer Penguin Measurements") %>%
  fmt_number(
    columns = c(mean_bill_length, mean_bill_depth, mean_flipper_length),
    decimals = 2
  ) %>%
  cols_label(
    species = "Species",
    mean_bill_length = "Bill Length (mean)",
    mean_bill_depth = "Bill Depth (mean)",
    mean_flipper_length = "Flipper Length (mean)"
  )
Palmer Penguin Measurements
Species Bill Length (mean) Bill Depth (mean) Flipper Length (mean)
Adelie 38.79 18.35 189.95
Chinstrap 48.83 18.42 195.82
Gentoo 47.51 14.98 217.19

Adding a Column Label

penguins_summary %>%
  gt() %>%
  tab_header(title = "Palmer Penguin Measurements") %>%
  tab_spanner(
    label = "Mean Measurements (mm)",
    columns = c(mean_bill_length, mean_bill_depth, mean_flipper_length)
  ) %>%
  cols_label(
    species = "Species",
    mean_bill_length = "Bill Length",
    mean_bill_depth = "Bill Depth",
    mean_flipper_length = "Flipper Length"
  )
Palmer Penguin Measurements
Species
Mean Measurements (mm)
Bill Length Bill Depth Flipper Length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Changing Alignment

penguins_summary %>%
  gt() %>%
  tab_header(title = "Palmer Penguin Measurements") %>%
  tab_spanner(
    label = "Mean Measurements (mm)",
    columns = c(mean_bill_length, mean_bill_depth, mean_flipper_length)
  ) %>%
  cols_label(
    species = "Species",
    mean_bill_length = "Bill Length",
    mean_bill_depth = "Bill Depth",
    mean_flipper_length = "Flipper Length"
  ) %>%
  cols_align(
    align = "center",
    columns = everything()
  )

Changing Alignment

Palmer Penguin Measurements
Species
Mean Measurements (mm)
Bill Length Bill Depth Flipper Length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Apply Color Themes to Table Cells

penguins_summary %>%
  gt() %>%
  data_color(
    columns = c(mean_bill_length, mean_flipper_length),
    fn = scales::col_numeric(palette = "Blues", domain = NULL)
  )
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Customize Font Sizes and Styles

penguins_summary %>%
  gt() %>%
  tab_options(
    table.font.size = px(14),
    table.font.names = "Arial"
  )
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Add Row Highlighting Based on Conditions

penguins_summary %>%
  gt() %>%
  tab_style(
    style = cell_fill(color = "lightgray"),
    locations = cells_body(
      rows = mean_flipper_length > 200
    )
  )
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Add Borders for Columns or Rows

penguins_summary %>%
  gt() %>%
  tab_style(
    style = cell_borders(
      sides = c("top", "bottom"),
      color = "black",
      weight = px(2)
    ),
    locations = cells_column_labels()
  )
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Another example

penguins_summary %>%
  gt() %>%
  tab_style(
    style = cell_borders(
      sides = c("top", "bottom"),
      color = "black",
      weight = px(2)),
    locations = cells_column_labels())%>%
  tab_style(
    style = cell_borders(
      sides = c("bottom"),
      color = "black",
      weight = px(2)),
    locations = cells_body(rows = nrow(penguins_summary)))%>%
    tab_options(
    table.border.top.style = "none",
    table.border.bottom.style = "none",
    table_body.hlines.style = "none")

Another example

species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Add Custom Row Labels

penguins_summary %>%
  gt(rowname_col = "species") %>%
  tab_stubhead(label = "Penguin Species")
Penguin Species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Format Numbers with Patterns and Decimals

penguins_summary %>%
  gt() %>%
  fmt_number(
    columns = c(mean_bill_length, mean_bill_depth, mean_flipper_length),
    decimals = 2,
    pattern = "{x} mm"
  )
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.79 mm 18.35 mm 189.95 mm
Chinstrap 48.83 mm 18.42 mm 195.82 mm
Gentoo 47.51 mm 14.98 mm 217.19 mm

Add Group Labels and Spanners

penguins_summary %>%
  gt() %>%
  tab_spanner(
    label = "Bill Measurements (mm)",
    columns = c(mean_bill_length, mean_bill_depth)
  ) %>%
    tab_spanner(
    label = "Flipper Measurements (mm)",
    columns = c(mean_flipper_length)
  ) %>%
    cols_label(
    species = "Species",
    mean_bill_length = "Length",
    mean_bill_depth = "Depth",
    mean_flipper_length = "Length"
  )

Add Group Labels and Spanners

Species
Bill Measurements (mm)
Flipper Measurements (mm)
Length Depth Length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Add Custom Footnotes

penguins_summary %>%
  gt() %>%
  tab_footnote(
    footnote = "Data represents mean values per species.",
    locations = cells_column_labels(
      columns = c(mean_bill_length, mean_flipper_length)
    )
  )
species mean_bill_length1 mean_bill_depth mean_flipper_length1
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187
1 Data represents mean values per species.

Add visualization to the table

penguin_trends %>%
  gt(rowname_col = "species") %>%
  cols_label(
    Year_2007 = "2007", # Adjust based on actual years in the dataset
    Year_2008 = "2008", # Adjust based on actual years in the dataset
    Year_2009 = "2009", # Adjust based on actual years in the dataset
    trend = "Density"
  ) %>%
  gt_plt_dist(
    column = trend, # Specify the column with the list of values
    type = "density" # Use density plots
  ) %>%
  tab_header(
    title = "Penguin Bill Length Trends",
    subtitle = "Mean bill lengths by year and density plot of all values"
  )

Add visualization to the table

Penguin Bill Length Trends
Mean bill lengths by year and density plot of all values
2007 2008 2009 Density
Adelie 38.82449 38.56000 38.98269
Chinstrap 48.72308 48.70000 49.05417
Gentoo 47.01471 46.93696 48.50000

kable + kableExtra

A Simple Table

library(kableExtra)
penguin_summary %>%
  kable() %>%
  kable_styling()
species Year_2007 Year_2008 Year_2009
Adelie 38.82449 38.56000 38.98269
Chinstrap 48.72308 48.70000 49.05417
Gentoo 47.01471 46.93696 48.50000

Grouping Rows

penguins_summary %>%
  group_by(species) %>%
  kable() %>%
  kable_styling() %>%
  add_header_above(c("Species Data" = 4))
Species Data
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Adding a Caption

penguins_summary %>%
  kable(caption = "Penguin Bill Measurements") %>%
  kable_styling()
Penguin Bill Measurements
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Adding Row Numbers

penguins_summary %>%
  kable(row.names = TRUE) %>%
  kable_styling()
species mean_bill_length mean_bill_depth mean_flipper_length
1 Adelie 38.791 18.346 189.954
2 Chinstrap 48.834 18.421 195.824
3 Gentoo 47.505 14.982 217.187

Adding a Column Header Spanning Multiple Columns

penguins_summary %>%
  kable() %>%
  add_header_above(c(" " = 1, "Bills" = 2, "Flippers"))
Bills
Flippers
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Adding Borders to the Table

penguins_summary %>%
  kable() %>%
  kable_styling(full_width = FALSE) %>%
  column_spec(1, border_right = TRUE)
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Adding Bold and Italic Text

penguins_summary %>%
  kable() %>%
  row_spec(1, bold = TRUE) %>%
  row_spec(3, italic = TRUE)
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Adding Multiple Footnotes

species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187
Note:
Measurements are in millimeters.
1 Note 1 with numbers.
2 Note 2 with numbers.
a Note 1 with letters.
b Note 2 with letters.
* Note 1 with symbols.
Note 2 with symbols.

Adding a Light Theme

penguins_summary %>%
  kable() %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Adding Header with Style

penguins_summary %>%
  kable() %>%
  kable_styling() %>%
  add_header_above(c(" " = 1, "Measurements" = 3)) %>%
  row_spec(0, bold = TRUE, color = "white", background = "black")
Measurements
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Highlighting Specific Rows

penguins_summary %>%
  kable() %>%
  row_spec(2, bold = TRUE, color = "red")
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Coloring Columns

penguins_summary %>%
  kable() %>%
  column_spec(2, color = "red")
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

Customizing Font Size

penguins_summary %>%
  kable() %>%
  kable_styling(font_size = 20)
species mean_bill_length mean_bill_depth mean_flipper_length
Adelie 38.791 18.346 189.954
Chinstrap 48.834 18.421 195.824
Gentoo 47.505 14.982 217.187

reactablefmtr

A Simple Reactable Table

library(reactable)
library(reactablefmtr)

reactable(penguins_summary)

Adding Column Labels

reactable(
  penguins_summary,
  columns = list(
    mean_bill_length = colDef(name = "Mean Bill Length (mm)"),
    mean_bill_depth = colDef(name = "Mean Bill Depth (mm)"),
    mean_flipper_length = colDef(name = "Mean flipper Length (mm)")))

Adding Conditional Formatting (Color Scales)

color_set <- c("#f7c844","#429460","#2e6d9e")

reactable(
  penguins_summary,
  columns = list(
    mean_bill_length = colDef(
      cell = color_tiles(penguins_summary, 
                         color_set))))

Adding Conditional Formatting (Color Scales)

Highlighting Specific Rows

reactable(
  penguins_summary,
  rowStyle = function(index) {
    if (penguins_summary$mean_bill_length[index] > 45) {
      list(background = "lightblue")
    } else {
      NULL
    }
  }
)

Highlighting Specific Rows

Adding Row Names

reactable(
  penguins_summary,
  rownames = TRUE
)

Adding Row Names

Adding Borders to the Table

reactable(
  penguins_summary,
  bordered = TRUE
)

Adding Pagination

reactable(
  penguins,
  pagination = TRUE,
  defaultPageSize = 3
)

Adding Pagination

Adding Bars

reactable(
  penguins_summary,
  columns = list(
    mean_flipper_length = colDef(
      maxWidth = 280,
      align = "center",
      cell = data_bars(
        data = penguins_summary,
        fill_color = c("#f7c844", "#429460", "#2e6d9e"),
        number_fmt = scales::number_format(accuracy = 0.2)
      ),
      style = list(borderLeft = "1px dashed rgba(0, 0, 0, 0.3)")
    )
  )
)

Adding Bars

Adding a Search Box